package com.proscenic.ops.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

enum ResultCode {
    SUCCESS(200, "操作成功"),
    FAILED(500, "操作失败"),
    VALIDATE_FAILED(404, "参数检验失败"),
    UNAUTHORIZED(401, "暂未登录或token已经过期"),
    FORBIDDEN(403, "没有相关权限");

    private long code;
    private String message;

    ResultCode(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public long getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

/**
 * 返回结果封装
 * @version 1.0
 * created by chenxin on 2021/8/25 10:19
 */
@ApiModel(description = "结果")
@Data
public class ResultVO<T> {
    @ApiModelProperty(value = "状态码，200：操作成功，500：操作失败，404：参数校验失败，401：未登录或登录失效，403：没有想过权限")
    private long code;

    @ApiModelProperty(value = "描述信息")
    private String message;

    @ApiModelProperty(value = "数据")
    private T data;

    protected ResultVO(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回结果
     * @author chenxin
     * @param data  获取的数据
     */
    public static <T> ResultVO<T> success(T data){
        return new ResultVO<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     * @author chenxin
     * @param messgae   提示信息
     * @param data      获取的数据
     */
    public static <T> ResultVO<T> success(String messgae, T data){
        return new ResultVO<T>(ResultCode.SUCCESS.getCode(), messgae, data);
    }

    private static <T> ResultVO<T> failed(ResultCode resultCode){
        return new ResultVO<T>(resultCode.getCode(), resultCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     * @author chenxin
     */
    public static <T> ResultVO<T> failed(){
        return failed(ResultCode.FAILED);
    }

    /**
     * 失败返回结果
     * @author chenxin
     */
    public static <T> ResultVO<T> failed(String message){
        return new ResultVO<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 参数校验失败返回结果
     * @author chenxin
     */
    public static <T> ResultVO<T> validateFailed(){
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 未登录返回结果
     * @author chenxin
     */
    public static <T> ResultVO<T> unauthorized(){
        return failed(ResultCode.UNAUTHORIZED);
    }

    /**
     * 未授权返回结果
     * @author chenxin
     */
    public static <T> ResultVO<T> forbidden(T data){
        return failed(ResultCode.FORBIDDEN);
    }
}
